package com.xiangwei.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiangwei.controller.AdminController;
import com.xiangwei.dao.AdminMapper;
import com.xiangwei.entity.Admin;
import com.xiangwei.service.IAdminService;
import com.xiangwei.util.Common;
import com.xiangwei.util.Const;
import com.xiangwei.util.MD5keyBean;
import com.xiangwei.util.StringUtils;

@Service
public class AdminServiceImpl implements IAdminService {
	private static final Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);
	
    @Resource
    private AdminMapper adminMapper;

    public AdminMapper getAdminMapper() {
        return adminMapper;
    }

    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    @Transactional
    public void insertAdmin(Admin admin) {
    	//admin.setPwd(EncrypDES.getTwiceEncString(admin.getPwd(), "xwgroup"));
    	adminMapper.insert(admin);
    }

	@Override
	public Admin getAdmin(Admin admin) {
		return adminMapper.findAdmin(admin);
	}

	@Override
	public List<Admin> findAdminList(Admin admin) {
		List<Admin> adminList = new ArrayList<Admin>();
		int role = admin.getRole();
		Admin a = adminMapper.selectByPrimaryKey(admin.getId());
		if(role == 0){//普通账户
			adminList.add(a);			
		}
		if(role == 1){//超级账户
			adminList = adminMapper.findAdminList();			
			adminList.add(a);
		}
		if(role == 2){//开发者账户
			adminList = adminMapper.findAdminAll();	
		}
		return adminList;
	}

	@Override
	public Admin selectByPrimaryKey(String id) {
		if(!Common.isEmptyString(id)){
			logger.error("传入参数id为空");
			return null;
		}
		return adminMapper.selectByPrimaryKey(Short.parseShort(id));
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		if(!Common.isEmptyString(id)){
			logger.error("传入参数id为空");
			return 0;
		}
		return adminMapper.deleteByPrimaryKey(Short.parseShort(id));
	}

	@Override
	public int savePwd(Admin admin) {
		String pwd = admin.getPwd().toUpperCase();
		admin.setPwd(pwd);
		return adminMapper.updateByPrimaryKeySelective(admin);
	}
	@Override
	public int saveAdmin(Admin admin) {
		if(Common.isNullOrEmpty(admin)){
			logger.error("传入参数为空");
			return 0;
		}
		
		//如果用户ID为空，则执行新增操作
		if(Common.isNullOrEmpty(admin.getId()) || !Common.isEmptyString(admin.getId()+"")){
			if(adminMapper.findUserNameCount(admin.getUsername()) > 0){
				logger.error("用户名已存在");
				return 0;
			}
			MD5keyBean md5 = new MD5keyBean();
			//设置用户初始化密码为：123456
			admin.setPwd(md5.getkeyBeanofStr(Const.Common.ADMIN_PASSWORD_INIT));
			return adminMapper.insertSelective(admin);
		//进行更新操作
		}else{
			Admin ad = adminMapper.selectByPrimaryKey(admin.getId());
			int count = adminMapper.findUserNameCount(admin.getUsername());
			if( count > 1 || (count==1 && !ad.getUsername().equals(admin.getUsername()))){
				logger.error("用户名已存在");
				return 0;
			}
			return adminMapper.updateByPrimaryKeySelective(admin);
		}
	}

	@Override
	public int updateByPrimaryKeySelective(Admin record) {
		return adminMapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int checkPwd(Admin admin, String oldPassword) {
		admin.setPwd(oldPassword.toUpperCase());
		Admin ad = adminMapper.findAdmin(admin);
		if(ad != null){
			return 1;
		}else{
			return 0;
		}
	}
	
	@Override
	public int findUserNameCount(String username,String id) {
		if(!Common.isEmptyString(username)){//判断参数是否为空
			return -1;
		}
		int count = adminMapper.findUserNameCount(username);
		if(count == 0){
			return 0;
		}
		Admin ad = adminMapper.selectByPrimaryKey(Short.parseShort(id));
		if(count == 1 && username.equals(ad.getUsername())){
			return 0;
		}
		return -1;
	}

}
